package com.li.sssp.handle;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.li.sssp.entity.Actor;
import com.li.sssp.entity.Module;
import com.li.sssp.entity.ModuleMast;
import com.li.sssp.entity.Role;
import com.li.sssp.general.general_url;
import com.li.sssp.imp.Json;
import com.li.sssp.inter.IJson;
import com.li.sssp.server.ActorService;
import com.li.sssp.server.ModuleMastService;
import com.li.sssp.server.ModuleService;
import com.li.sssp.server.RoleService;

/**
 * 关于role actor module 的所有controller层的方法
 * 
 * @author guazi
 * @date 2017/03/09
 * 
 */
@Controller
@RequestMapping("role")
public class RoleHandle {

	@Autowired
	private RoleService roleService;
	@Autowired
	private ActorService actorService;
	@Autowired
	private ModuleMastService moduleMastService;
	@Autowired
	private ModuleService moduleService;
	@Autowired
	private general_url murl;

	private IJson js;
	{
		js = new Json();
	}
	
	
	
	/**
	 * actor修改跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "actoredit/{id}")
	public String actoredit(@PathVariable(value = "id") String Str_id, Map<String, Object> map) {
		
		Integer id=0;
		try {
			id=Integer.parseInt(Str_id);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		map.put("actorName", actorService.getbyid(id).getName());
		map.put("id", Str_id);
		
		return "role/rinput";
	}

	/**
	 * Ajax提交Roles数据到数据库中
	 * 
	 * @param roles
	 * @return
	 */
	@RequestMapping(value = "AjaxForRoles")
	@ResponseBody
	public String role(@RequestBody List<Role> roles) {
		//	判断actor的权限是否存在
		if(roles.size()>0){ //	删除存在actor的原权限
			roleService.DelByActor(roles.get(0).actor);
		}
		// 提交保存
		for (Role role : roles) {
			// 找一下有没有相同的数据
			List<Role> _roles = roleService.findRole(role.actor, role.module,role.getC());
			if (_roles.size() > 0) {	//	如存在相同的，需要把原来的id赋值
				role.setId(_roles.get(0).getId());
			}
			roleService.save(role);	//存储
		}
		return "success";
	}

	

	/**
	 * 角色的列表
	 * 
	 * @param map
	 * @param lastName
	 * @param str_pageNo
	 * @param str_pageIndex
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "actor")
	public String Actors(Map<String, Object> map,
			@RequestParam(value = "search", required = false, defaultValue = "") String lastName,
			@RequestParam(value = "pageNo", required = false, defaultValue = "1") String str_pageNo,
			@RequestParam(value = "pageIndex", required = false, defaultValue = "8") String str_pageIndex,
			HttpServletRequest request) {

		// 提取page的参数
		int pageNo = 1;
		int pageIndex = 5;
		// 获取当前url
		// String url = getUrl(request);
		String url = murl.getUrl(request);
		try {
			pageNo = Integer.parseInt(str_pageNo);
			pageIndex = Integer.parseInt(str_pageIndex);
		} catch (Exception e) {
			// TODO: handle exception
		}

		// 获取所有role数据
		map.put("actors", actorService.getAll(new PageRequest(pageNo-1, pageIndex)));
		map.put("pageNo", pageNo);
		map.put("url", url);
		return "role/rlist";
	}

	
	
	/**
	 * ajax 访问 type:post ajaxForRole 
	 * 查找所有role的数据 为修改权限时自动构建UE时用，返回Json格式
	 * 
	 * @param request
	 * @param response
	 * @throws JsonProcessingException
	 */
	@RequestMapping(value = "ajaxForRole", method = RequestMethod.POST)
	public void ajaxForRole(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
		// actor_id;
		Integer actor_id = 0;
		try {
			actor_id = Integer.parseInt(request.getParameter("actor_id"));
		} catch (Exception e) {
			// TODO: handle exception
		}
		//	根据角色id查找该角色所有权限
		List<Role> ls = roleService.findByActor(actorService.getbyid(actor_id));

		//	利用Jackson来进行序列化转为Json
		ObjectMapper mapper = new ObjectMapper();
		js.renderData(response, mapper.writeValueAsString(ls));
	}

	
	
	/**
	 * ajax访问，取得所有基础模块数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping(value = "ajaxForModuleMast", method = RequestMethod.POST)
	@ResponseBody
	public String ajaxForModuleMast(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
		
		List<ModuleMast> ls = moduleMastService.GetAll();
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(ls);
		
	}

	/**
	 * ajax，根据高级模块查找相应的基础模块
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "ajaxForModule", method = RequestMethod.POST)
	public void ajaxForModule(HttpServletRequest request, HttpServletResponse response) {
		
		// 根据mastid获取modulemast
		Integer mast_id = 0;
		try {
			mast_id = Integer.parseInt(request.getParameter("mast_id"));
		} catch (Exception e) {
			// TODO: handle exception
		}

		ModuleMast moduleMast = moduleMastService.GetById(mast_id);
		List<Module> ls = moduleService.getModuleByModuleMast(moduleMast);
		js.renderData(response, JSON.toJSONString(ls, SerializerFeature.DisableCircularReferenceDetect));
	}

	/**
	 * ajax 取得所有模块
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "ajaxForModuleAll", method = RequestMethod.POST)
	public void ajaxForModuleAll(HttpServletRequest request, HttpServletResponse response) {
		List<Module> ls = moduleService.getAll();
		js.renderData(response, JSON.toJSONString(ls, SerializerFeature.DisableCircularReferenceDetect));
	}

}
